package com.snake.vargoappstore.widget;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.support.v4.content.LocalBroadcastManager;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;

import com.snake.mylibrary.utils.CommonUtil;

/**配合TabView使用，带消息提示的指示器*/
public class TabItem extends View{
    private Bitmap mIconSelect;
    private Bitmap mIconNormal;
    private String mTextValue ;
    private Paint mIconPaintNormal;
    private Paint mIconPaintSelect;
    private Paint mTipsBgPaint;
    private Paint mTipsTextPaint;
    private Paint mTextPaintNormal;
    private Paint mTextPaintSelect;
    private int mTextColorNormal;
    private int mTextColorSelect;
    private float mWidth;
    private float mHeight;
    private int tipCount;
    private int mTextSize;//sp单位
    private Rect mTipsTextBoundsRect;
    private Rect mTextBoundsRect;

    public static final String TABITEM_TIP = "tabItem_tip";
    private TipReceiver mReceiver;


    public TabItem(Context context) {
        super(context);
        init();
    }

    public TabItem(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public TabItem(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        //icon画笔Normal
        mIconPaintNormal = new Paint();
        mIconPaintNormal.setAntiAlias(true);
        //icon画笔Select
        mIconPaintSelect = new Paint();
        mIconPaintNormal.setAntiAlias(true);
        //字体画笔Normal状态
        mTextPaintNormal = new Paint();
        mTextPaintNormal.setAntiAlias(true);//图形抗锯齿
        mTextPaintNormal.setTextSize(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP,mTextSize,getResources().getDisplayMetrics()));//设置字体的大小。用sp设置。
        mTextPaintNormal.setTextAlign(Paint.Align.CENTER);//对齐方式，相对于坐标点。有左,右,中心三种。
        //字体画笔Select状态
        mTextPaintSelect = new Paint();
        mTextPaintSelect.setAntiAlias(true);//图形抗锯齿
        mTextPaintSelect.setTextAlign(Paint.Align.CENTER);//对齐方式，相对于坐标点。有左,右,中心三种。

        //消息提示背景
        mTipsBgPaint = new Paint(Paint.ANTI_ALIAS_FLAG) ;
        mTipsBgPaint.setAlpha(0xff);
        mTipsBgPaint.setColor(Color.RED);
        //绘制消息条数字体画笔
        mTipsTextPaint = new Paint() ;
        mTipsTextPaint.setAlpha(0xff);
        mTipsTextPaint.setColor(Color.WHITE);
        mTipsTextPaint.setAntiAlias(true);
        mTipsTextPaint.setTextAlign(Paint.Align.CENTER);
        mTipsTextPaint.setTextSize(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP,9, getResources().getDisplayMetrics()));

    }

    private void getTextBounds() {
        mTextBoundsRect = new Rect();
        mTextPaintNormal.getTextBounds(mTextValue,0,mTextValue.length(),mTextBoundsRect);
    }

    private void getTipsTextBounds() {
        mTipsTextBoundsRect = new Rect();
        mTipsTextPaint.getTextBounds(String.valueOf(tipCount),0,String.valueOf(tipCount).length(),mTipsTextBoundsRect);
    }
    /**
     *测量内容大小。设置默认warp_content值
     * */
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);
        int width = 0;
        int height = 0;

        measureText();
        //内容的宽度
        int contentWidth = Math.max(mIconNormal.getWidth(),mTextBoundsRect.width());
        int desiredWidth = getPaddingLeft()+getPaddingRight()+contentWidth;
        switch(widthMode){
            case MeasureSpec.EXACTLY:
                width = widthSize;
                break;
            case MeasureSpec.AT_MOST:
                width = Math.min(desiredWidth,widthSize);
                break;
            case MeasureSpec.UNSPECIFIED:
                width = desiredWidth;
                break;
        }
        //内容的高度
        int contentHeight = mTextBoundsRect.height()+mIconSelect.getHeight()+5;
        int desiredHeight = getPaddingTop()+getPaddingBottom()+contentHeight;

        switch(heightMode){
            case MeasureSpec.EXACTLY:
                height = heightSize;
                break;
            case MeasureSpec.AT_MOST:
                height = Math.min(desiredHeight,heightSize);
                break;
            case MeasureSpec.UNSPECIFIED:
                height = desiredHeight;
                break;
        }
        setMeasuredDimension(width, height);
    }

    private void measureText() {
        getTextBounds();
        getTipsTextBounds();
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        mWidth = w;
        mHeight = h;
//        ExceptionUtil.throwException();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //1.画图标，２。画文字。３．画红点
        drawBitmap(canvas);
        drawText(canvas);
        if(tipCount>0){
            drawTips(canvas);
        }
    }

    private void drawBitmap(Canvas canvas) {
        float left = (mWidth-mIconSelect.getWidth())/2.0f;//水平居中
        float top = (mHeight-mIconSelect.getHeight()-mTextBoundsRect.height())/2.0f;//垂直居中
        canvas.drawBitmap(mIconNormal,left,top, mIconPaintNormal);
        canvas.drawBitmap(mIconSelect,left,top, mIconPaintSelect);
    }
    private void drawText(Canvas canvas) {
        float x = mWidth/2.0f;//字体中心x坐标
        float y = (mHeight+mIconSelect.getHeight()+mTextBoundsRect.height())/2.0f;//字体中心y坐标()
        canvas.drawText(mTextValue,x,y, mTextPaintNormal);
        canvas.drawText(mTextValue,x,y, mTextPaintSelect);
    }
    private void drawTips(Canvas canvas) {
        canvas.drawCircle(mWidth/4*3.0f,mHeight/4.0f, CommonUtil.dp2px(getContext(),7),mTipsBgPaint);
        canvas.drawText(String.valueOf(tipCount),mWidth/4*3.0f,mHeight/4.0f+mTipsTextBoundsRect.height()/2.0f,mTipsTextPaint);
    }

    public void setTextSize(int size){
        mTextSize = size;
        mTextPaintNormal.setTextSize(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP,mTextSize,getResources().getDisplayMetrics()));//设置字体的大小。用sp设置。
        mTextPaintSelect.setTextSize(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP,mTextSize,getResources().getDisplayMetrics()));//设置字体的大小。用sp设置。
    }

    public void setTextColorNormal(int textColor){
        mTextColorNormal = textColor;
        mTextPaintNormal.setColor(mTextColorNormal);
        mTextPaintNormal.setAlpha(0xff);
    }
    public void setTextColorSelect(int textColor){
        mTextColorSelect = textColor;
        mTextPaintSelect.setColor(mTextColorSelect);
        mTextPaintSelect.setAlpha(0xff);
    }

    public void setIconAndText(int[] ids,String textValue){
        mIconSelect = BitmapFactory.decodeResource(getResources(), ids[0]);
        mIconNormal = BitmapFactory.decodeResource(getResources(), ids[1]);
        mTextValue = textValue;
    }

    public void setTabAlpha(float alpha){
        int paintAlpha = (int)(alpha*255) ;
        mIconPaintSelect.setAlpha(paintAlpha);
        mTextPaintSelect.setAlpha(paintAlpha);
        mIconPaintNormal.setAlpha(255-paintAlpha);
        mTextPaintNormal.setAlpha(255-paintAlpha);
        invalidate();
    }
    /**更改消息提示个数*/
    private void setTipCount(int count){
        tipCount = count;
        invalidate();
    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        mReceiver = new TipReceiver();
        int index = (int) this.getTag();
        /**注册通信事件，通过自身tag来标识*/
        LocalBroadcastManager.getInstance(getContext()).registerReceiver(mReceiver,new IntentFilter(TABITEM_TIP+index));
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        /**注销通信事件*/
        LocalBroadcastManager.getInstance(getContext()).unregisterReceiver(mReceiver);
    }
    
    class TipReceiver extends BroadcastReceiver{
        @Override
        public void onReceive(Context context, Intent intent) {
            int tipCount = intent.getIntExtra("tip", 0);
            setTipCount(tipCount);
        }
    }
    
    
    
}
